﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;

namespace CoolCode.Web.UI.Views.IC
{
    public partial class ProductOutResult : AuthPage
    {
        private IInspectionService _Service;
        public IInspectionService Service
        {
            get
            {
                if (_Service == null)
                {
                    _Service = ServiceFactory.Produce<IInspectionService>();
                }
                return _Service;
            }
        }

        protected override bool CheckUserRole()
        {
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            string key = RequestUtility.GetQueryString("key");
            if (string.IsNullOrEmpty(key))
            {
                lnkExportExcel.Enabled = false;
                udpHeader.Alert("发生错误：出库结果丢失！");
                return;
            }
            if (!IsPostBack)
            {
                BindData_Master(new PageParam());
            }
            lnkExportExcel.NavigateUrl = string.Format("~/WebServices/ProductHandler.ashx?type=excel&key={0}", key);
            lnkExportExcel.Enabled = true;
        }

        protected void handleEvents()
        {
            GridViewHelper.HighlightStatus(gvMaster);
            pgrMaster.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging);
        }

        private void BindData_Master(PageParam p)
        {
            string key = RequestUtility.GetQueryString("key");
            if (string.IsNullOrEmpty(key)) return;
            Guid guid = new Guid(key);
            var list = Service.PagingOutbound(guid, p);
            gvMaster.DataSource = list;
            gvMaster.DataBind();
            pgrMaster.Bind(list);
        }


        //分页控件的分页事件
        protected void pgrMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindData_Master(new PageParam(e.NewPageIndex));
        }
    }
}
